package pro.shushi.pamirs.hr.core.helper;

import pro.shushi.pamirs.hr.api.enums.HrSimpleExpEnum;
import pro.shushi.pamirs.meta.common.exception.PamirsException;

import java.util.regex.Pattern;

/**
 * @author shushi
 * @date 动作API名称校验
 */
public class ActionNameChecker {

    private final static Pattern pattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]{2,48}[a-zA-Z0-9]$");

    public static void checkActionName(String name) {

        if (name.contains("/")) {
            throw PamirsException.construct(HrSimpleExpEnum.ACTION_NAME_CHECK_ERROR).errThrow();
        }
        boolean matchesName = matchesName(name);
        if (!matchesName) {
            throw PamirsException.construct(HrSimpleExpEnum.ACTION_NAME_CHECK_ERROR).errThrow();
        }
    }

    private static boolean matchesName(String name) {
        return pattern.matcher(name).matches();
    }

}
